<?php
class UnitAction extends IntesysAction{
    public function index(){
        $this->integraltype=D('Integral_type')->where('site_id ='.__SITEID__)->field('id,remark')->select();

        $this->display();
    }
    public function chart(){
        if($this->_param('pid') && $this->_param('pid')!=''){
            $this->pid=$this->_param('pid');
        }else{
            if($this->_param('mid')&&$this->_param('mid')!=''){
                $this->mid=$this->_param('mid');
            }else{
                $positionid=D('Member')->where('id ='.$this->memberInfo['id'])->getfield('memberpositionid');
                $positionid=array_filter(explode(',',$positionid));
                $positionid=$positionid['0'];//默认自己的第一个部门
                if(empty($positionid)){//或者为公司第一个部门
                    $positionid=D('Member_position')->where('site_id ='.__SITEID__)->getfield('id');
                }
                $this->pid=$positionid;
            }
        }
        if($this->_param('integraltype')){
            $this->integraltype = $this->_param('integraltype');
        }else{
            $this->integraltype = D('Integral_type')->where('typename="Binte" and site_id ='.__SITEID__)->getfield('id');
        }
        if($this->_param('io')&&$this->_param('io')!=''){
            $this->io=$this->_param('io');
        }else{
            $this->io=1;
        }
        if($this->_param('time')&&$this->_param('time')!=''){
            $this->time=$this->_param('time');
        }else{
            $this->time='week';
        }
        if($this->pid){
            $this->allmember=$this->dataOrigin($this->pid,$this->time,$this->integraltype,$this->io,0);//0 团队
        }elseif($this->mid){
            $this->allmember=$this->dataOrigin($this->mid,$this->time,$this->integraltype,$this->io,1);//1 个人
        }
        foreach($this->allmember as $k =>$v){//总积分，统计比例
            static $sum=0;
            $sum+=abs($v['sum']);
        }
        foreach($this->allmember as $key =>$val){
            $part=round(abs($val['sum'])/$sum*100,2);
            $name[]="{value:".$part.",label:'".$val['name']."',formatted:'".$part."%'}";
            $legend[]=array('name'=>$val['name'],'sum'=>$val['sum']);
        }
        $this->legend=$legend;
//        var_dump($this->legend);
        $this->sourcename=implode(',',$name);

        $this->display();
    }
    //积分来源统计
    public function dataOrigin($id,$time,$integraltype='173',$plusMinus,$symbol='0'){
        $end=time();
        if($time=='week'){
            $date=new DateTime();//use the php origin class
            $date->modify('this week');
            $start=strtotime($date->format('Y-m-d 00:00:00'));//week start
            $map["dateline"]=array('between',array($start,$end));
        }elseif($time=='month'){
            $start=strtotime(date('Y')."-".date('m')."-1");
            $map["dateline"]=array('between',array($start,$end));
        }elseif($time=='year'){
            $start=strtotime(date('Y')."-1-1");
            $map["dateline"]=array('between',array($start,$end));
        }
        if($symbol==0){//department
			$map['memberpositionid'] = array('like', '%,'.$id.',%');
        }elseif($symbol==1){//personal
            $map['member_id']=$id;
        }
        switch($plusMinus){
            case -1:$map['num']=array('lt',0);break;
            case 1 :$map['num']=array('gt',0);break;
        }
//        var_dump($map);
        $map['integraltypeid']=$integraltype;
        $loglist=D('Integralranking')->where($map)->group('formcategory_id')->field('sum(num) as sum,formcategory_id')->select();
        foreach($loglist as $k =>$v){
            $loglist[$k]['name']=D('Formcategory')->where('id ='.$v['formcategory_id'])->getfield('name');
        }
        return $loglist;


    }
}